import AntModal from '@/components/pt/dialog/AntModal'
import { updateBizParts, getBizParts } from '@/api/biz/bizParts'
import { upload } from '@/api/system/upload'
import storage from 'store'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import BizAccessoryListForm from '@/views/biz/bizparts/modules/BizAccessoryListForm'
import BizPartsAddForm from '@/views/biz/bizparts/modules/BizPartsAddForm'
import { getInitData } from '@/api/biz/bizParts'
export default {
  name: 'AccessoryForm',
  components: {
    AntModal,
    BizAccessoryListForm,
    BizPartsAddForm
  },
  data() {
    return {
      showProductModal: false,
      showPlanModal: false,
      activeKey: "1",
      uploadUrl: process.env.VUE_APP_BASE_API + "/common/upload",
      // 设置上传的请求头部
      headers: { Authorization: 'Bearer ' + storage.get(ACCESS_TOKEN) },
      multiple: false,
      multipleDetail: false,
      open: false,
      closeDialog: true,
      spinning: false,
      delayTime: 100,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      loading: false,
      disabled: true,
      total: 0,
      id: undefined,
      formTitle: '设置配件信息',
      // 表单参数
      form: { orderType: "1" },
      attachmentRefName: 'addUploaderFile', // 标志表单是否含有附件
      formId: '',
      currentType: '',//当前新增明细类型
      list: [],
      selectedRowKeys: [],
      selectedDetailRowKeys: [],
      selectedDetailRows: [],
      ids: [],
      fileList: [],
      bizAccessoryInfo: {},
      bizAccessoryId: '',
      showAddModal: false,
      statusOptions: [],

    }
  },
  filters: {},
  created() {
    getInitData('reference_state').then(response => {
      this.statusOptions = response.data.reference_state
    })
  },
  computed: {},
  watch: {},
  mounted() { },
  methods: {
    onClose() {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 取消按钮
    cancel() {
      console.log('cancel')
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        remark: undefined,
        orderId: undefined
      }
      this.fileList = []
      this.selectedRowKeys = []
    },

    modify() {
      this.disabled = false
    },
    /** 提交按钮 */
    submitForm: function (closeDialog) {
      this.closeDialog = closeDialog
      // this.disabled = true
      if (this.fileList.length == 0) {
        this.$message.warn('请上传配件图片');
      }
      let filePaths = "";
      this.fileList.forEach(file => {
        if (filePaths.length > 0) {
          filePaths += ",";
        }
        filePaths += file.url;
      })
      this.form.fileList = filePaths
      this.form.orderId = this.orderId
      updateBizParts(this.form).then(response => {
        this.$message.success('保存成功', 3)
        this.open = false
        this.$emit('ok')
        this.$emit('close')
      })
    },
    handleChange(info, e) {
      const formData = new FormData();
      formData.append('file', info.file);
      upload(formData).then(response => {
        const d = {
          uid: info.file.uid,
          name: response.fileName,
          url: response.url,
          status: 'done'
        }
        this.fileList.push(d);
      })
    },
    handleRemove(info) {
      this.fileList = this.fileList.filter(function (item) {
        return item.uid != info.uid
      })
    },

    /**
     * 关联测量单据
     */
    handleChooseAccessory() {
      this.$nextTick(() => {
        this.$refs.BizAccessoryListForm.handleChooseAccessory(this.bizAccessoryInfo)
      })
    },

    selectOk(rows) {
      this.form = rows[0]
      let fileList = (this.form && this.form.fileList || '').split(',')
      fileList.forEach((item, index) => {
        const d = {
          uid: index,
          name: item,
          url: item,
          status: 'done'
        }
        // 文件列表
        this.fileList.push(d)
      })
      this.$forceUpdate()
    },


    /** 新增按钮操作 */
    handleSetAccessory(row) {
      this.open = true
      this.orderId = row.id;
      this.fileList = [];
      this.spinning = !this.spinning
      this.bizAccessoryId = row.accessoryId
      this.bizAccessoryInfo = row
      if (this.bizAccessoryId) {
        getBizParts(this.bizAccessoryId).then(res => {
          if (res.code == 200) {
            this.form = res.data
            let fileList = (res.data && res.data.fileList || '').split(',')
            fileList.forEach((item, index) => {
              const d = {
                uid: index,
                name: item,
                url: item,
                status: 'done'
              }
              // 文件列表
              this.fileList.push(d)
              this.spinning = !this.spinning
            })
          } else {
            this.message.error(res.msg)
          }
        })
      }


    },

    handleAdd() {
      this.showAddModal = true
      this.$nextTick(() => (
        this.$refs.bizPartsAddForm.handleAddAccess(this.bizAccessoryInfo)
      ))
    },
    setAccessoryInfo(res) {
      if (res.code == 200) {
        let fileList = []
        this.fileList = []
        this.form = res.data
        if (res.data && res.data.fileList) {
          fileList = res.data.fileList.split(',')
        }
        fileList.forEach((item, index) => {
          const d = {
            uid: index,
            name: item,
            url: item,
            status: 'done'
          }
          // 文件列表
          this.fileList.push(d)
        })
        console.log('this.fileList :>> ', this.fileList);
      }
    }
  }
}
